package com.ruoyi.web.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.CartVo;
import com.ruoyi.system.domain.vo.IsProductOrderVo;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.domain.vo.SelfOrderVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SuperOrders;
import com.ruoyi.system.service.ISuperOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/order/orders")
public class SuperOrdersController extends BaseController
{
    @Autowired
    private ISuperOrdersService superOrdersService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperOrders superOrders)
    {
        startPage();
        List<SuperOrders> list = superOrdersService.selectSuperOrdersList(superOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:orders:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperOrders superOrders)
    {
        List<SuperOrders> list = superOrdersService.selectSuperOrdersList(superOrders);
        ExcelUtil<SuperOrders> util = new ExcelUtil<SuperOrders>(SuperOrders.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superOrdersService.selectSuperOrdersById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperOrders superOrders)
    {
        return toAjax(superOrdersService.insertSuperOrders(superOrders));
    }

    /**
     * 新增订单，并往订单明细添加数据
     */
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
//    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/myAdd")
    public AjaxResult myAdd(@RequestBody OrderVo order)
    {
        System.out.println(order);
//        superOrdersService.myAdd(order);
        return success(superOrdersService.myAdd(order));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('order:orders:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperOrders superOrders)
    {
        return toAjax(superOrdersService.updateSuperOrders(superOrders));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('order:orders:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superOrdersService.deleteSuperOrdersByIds(ids));
    }


    // 用户直接购物生成订单
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
    @PostMapping("/isProductOrder")
    public AjaxResult isProductOrder(@RequestBody IsProductOrderVo isProduct){
        System.out.println(isProduct);
        return success(superOrdersService.isProductOrder(isProduct));
    }

    // 查看用户支付后，自提订单
    @PreAuthorize("@ss.hasPermi('order:orders:list')")
    @GetMapping("/selfOrder/{userId}")
    public TableDataInfo selfOrder(@PathVariable Long userId){
        startPage();
        List<SelfOrderVo> list = superOrdersService.selfOrder(userId);
        list.forEach(l-> System.out.println(l));
        return getDataTable(list);
    }

    // 查看用户自提订单详情
    @PreAuthorize("@ss.hasPermi('order:orders:query')")
    @GetMapping("/selfOrderDetail/{id}")
    public AjaxResult selfOrderDetail(@PathVariable Long id){
        return success(superOrdersService.selfOrderDetail(id));
    }

    // 查看用户自提的商家地址
    @PreAuthorize("@ss.hasPermi('order:orders:query')")
    @GetMapping("/selfAddress/{id}")
    public AjaxResult selfAddress(@PathVariable Long id){
        return success(superOrdersService.selfAddress(id));
    }

    // 获取订单id的时间
    @PreAuthorize("@ss.hasPermi('order:orders:query')")
    @GetMapping("/selfTime/{id}")
    public AjaxResult getTime(@PathVariable Long id){
        return success(superOrdersService.getTime(id));
    }
}
